const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    curTap: 0,
    titleTabs: [{
      title: "全部",
      id: 0
    }, {
      title: "待付款",
      id: 1
    },
    {
      title: "待拼团",
      id: 2
    }, {
      title: "待发货",
      id: 3
    },
    {
      title: "待收货",
      id: 4
    },
    {
      title: "待评价",
      id: 5
    }
    ],
    goodsArr: [
      {
        id: 1,
        title: '商品商品商品商品商品商品商品商品商品商品商品商品商品商品商品商品商品商品商品商品商品商品商品商品1',
        num: 1,
        price: 10,
        selected: false,
        img: "../../images/goods-test.png"
      },
      {
        id: 2,
        title: '商品2',
        num: 2,
        price: 5,
        selected: true,
        img: "../../images/goods-test.png"
      },
      {
        id: 3,
        title: '商品3',
        num: 2,
        price: 15,
        selected: false,
        img: "../../images/goods-test.png"
      },
    ],
    opertionBtns: {
      0: [
        { name: '删除订单' },
        { name: '再次购买' },
        { name: '查看物流' },
        { name: '申请退换' }],
      1: [
        { name: '取消订单' },
        { name: '立即付款' }],
      2: [
        { name: '取消订单' },
        { name: '拼团详情' }],
      3: [],
      4: [
        { name: '再次购买' },
        { name: '查看物流' },
        { name: '确认收货' }],
      5: [
        { name: '再次购买' },
        { name: '查看物流' },
        { name: '申请退换' },
        { name: '立即评价' }],
    },
    hasMore: false,
    curPage: 1,
    pageSize: 6,
    items: [],
    showCancelDialog: false, //取消订单弹窗
    curPayType: 0,

    operType: 1, //1:取消订单  2：删除订单
    curOrder: {}, //按钮操作的当前id
    curOrderIndex: 0, //当前订单的索引
    emptyType: 2,
    // 订单类型
    orderType: 0,
    showCancelDialog: false, //取消订单弹窗
    maskShowModel:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.curTap;
    this.setData({
      curTap: id
    })
  },
  tabsSelect: function (e) {
    this.setData({
      curTap: e.currentTarget.dataset.id
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
   //上拉加载
  onReachBottom: function() {
    if (this.data.hasMore == false) return;
    let page = this.data.curPage + 1;
    this.queryOrderList(page, this.data.pageSize);
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //刷新页面数据
  refreshData: function () {
    let userInfo = app.globalData.userBean;
    if (!(userInfo && userInfo.userId)) {
      wx.navigateTo({
        url: '../../login/login-type/index' // 指定页面的url
      });
      return
    }
    var that = this;
    that.setData({
      hasMore: true
    })
    wx.pageScrollTo({
      scrollTop: 0, // 目标位置
      duration: 300
    });
    this.queryOrderList(1, this.data.pageSize)
  },
  //分页查询订单
  queryOrderList: function (page, pageSize) {
    let that = this;
    let userBean = app.globalData.userBean;
    let orderStatus = this.data.curTap;
    let prams = {
      pageSize: that.data.pageSize,
      pageNumber: page,
      userId: userBean.userId,
      status: orderStatus
    };
    api.queryOrderList(prams).then(res => {
      //请求成功
      that.querySuccess(res, page)
      wx.stopPullDownRefresh();
      that.setData({
        emptyType: 2
      })
    }).catch(msg => {
      wx.stopPullDownRefresh();
      that.setData({
        emptyType: 5
      })
      var message = typeof msg == "string" ? msg : msg.message;
      app.showToast(message);
    })
  },

  //查询成功
  querySuccess: function (res, curPage) {
    let that = this;
    //数据
    for (let i = 0; i < res.records.length; i++) {
      res.records[i] = util.formatOrderStatus(res.records[i])
      res.records[i].orderCnStatus = res.records[i].orderCnStatus;
    }
    let records = res.records;
    let maxPage = parseInt(res.pages);
    var datas = curPage == 1 ? [] : this.data.items;
    if (records) {
      datas = datas.concat(records);
    }
    that.setData({
      hasMore: maxPage > curPage,
      items: datas,
      curPage: curPage,
    })
  },
  // 按钮操作
  opertionTab: function (e) {
    let order = e.currentTarget.dataset.order;
    let orderindex = e.currentTarget.dataset.orderindex;
    let btnName = e.currentTarget.id;
    let opertionBtns = this.data.opertionBtns;
    this.setData({
      curOrder: order,
      curOrderIndex: orderindex
    })

    switch (btnName) {
      case "删除订单":
        this.setData({
          operType: 2
        })
        this.showCancelDialog();
        break;
      case "再次购买":
      wx.switchTab({
        url: '../../main-home/main-home' // 指定页面的url
      });
        break;
      case "查看物流":
      wx.navigateTo({
        url: '../logistics-detail/index' // 指定页面的url
      });
        break;
      case "申请退换":
         wx.navigateTo({
        url: '../../servers/after-sale-index/after-sale-index' // 指定页面的url
      });
        break;
      case "取消订单":
        this.setData({
          operType: 1
        })
        this.showCancelDialog();
        break;
      case "立即付款":
        wx.navigateTo({
          url: '../../pay-type/pay-type' // 指定页面的url
        });
        break;
      case "拼团详情":
       wx.navigateTo({
          url: '../../ping-detail/ping-detail' // 指定页面的url
        });
        break;
      case "确认收货":
        break;
      case "立即评价":
      wx.navigateTo({
        url: '../order-evaluation/index' // 指定页面的url
      });
        break;
    }
  },
   //请求删除or取消订单
  onConfirm: function() {
    let that = this;
    let curOrder = this.data.curOrder;
    let curOrderIndex = this.data.curOrderIndex;

    let operType = this.data.operType;
    if (operType == 2) {
      app.showLoading()
      api.deleteOrder(curOrder.orderId).then(res => {
        wx.hideLoading()
        this.refreshData();
      }).catch(msg => {
        wx.hideLoading()
        let message = typeof msg == "string" ? msg : msg.message;
        app.showToast(message);
      })
    } else {
      let promise=undefined
      if (curOrder.payStatus === 0) {
        promise = api.cancelOrder(curOrder.orderId)
      } else {
        promise = api.refundOrder(curOrder.orderId)
      }
      promise.then(res => {
        this.refreshData();
      }).catch(msg => {
        let message = typeof msg == "string" ? msg : msg.message;
        app.showToast(message);
      })
    }
  },
    //显示是否弹出取消订单的操作
  showCancelDialog: function() {
    this.setData({
      showCancelDialog: true,
      maskShowModel:true
    })
  },
   //隐藏所有弹窗
  hiddenDialog: function(e) {
    this.setData({
      showCancelDialog: false,
      maskShowModel:false
    })
  },
  toOrderDetail:function(){
    wx.navigateTo({
      url:'../order-detail/index'
    })
  }

})